//
//  RwduserAccountViewModel.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/12.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit

class RwduserAccountViewModel: NSObject {
    
    //创建一个单例
    static let shareUserAccount: RwduserAccountViewModel = RwduserAccountViewModel()
    //解档存储好的对象
    var userAccount: RwdUserAcount? {
        
        return RwdUserAcount.unArchiverUserData()
        
    }
    
    var isLogin:Bool {
        
        return accessToken != nil 
        
    }
    
    
    var accessToken:String? {
        //判断是否有值
        guard let token = userAccount?.access_token else {
            
            return nil
            
        }
        
        let result = userAccount?.expiresDate?.compare(NSDate())
        //降序
        if result == NSComparisonResult.OrderedDescending {
            
            return token
            
        }else{
            
            return nil
        }
        
    }
    
    
    private override init() {
        super.init()
        
    }
    func getAccessToken(code: String,callBack:(isSuccess:Bool)->()) {
        
        RwdNetworkTools.sharedTools.requestAccesstoken(code) { (response, error) -> () in
            //如果有错误信息返回 那么久打印出来
            if error != nil {
                
                print(error)
                //闭包传递bool类型的登录状态
                callBack(isSuccess: false)
                
                return
            }
            
            guard let dict = response as? [String:AnyObject] else {
                print("格式错误")
                //闭包传递bool类型的登录状态
                callBack(isSuccess: false)
                return
            }
            //如果走到这里说明 dict一定存在且有值
            let userAcount = RwdUserAcount(dict: dict)
            
            print(userAcount)
            
            self.getuserInfos(userAcount,callBack: callBack)
            
        }
        
    }
    
    
    
    func getuserInfos(userAcount:RwdUserAcount,callBack:(isSuccess:Bool)->()) {
        //通过网络工具类请求数据
        RwdNetworkTools.sharedTools.getuserInfos(userAcount) { (response, error) -> () in
            
            if error != nil {
                
                print(error)
                //闭包传递bool类型的登录状态
                callBack(isSuccess: false)
                return
                
            }
            //判断
            guard let dict = response as? [String:AnyObject] else {
                print("格式错误")
                //闭包传递bool类型的登录状态
                callBack(isSuccess: false)
                return
            }
            let name = dict["name"]
            let avatar_large = dict["avatar_large"]
            //设置用户名和头像
            userAcount.name = name as?String
            userAcount.avatar_large = avatar_large as?String
            
            //保存
            let result = userAcount.saveToUserData()
            
            if result {
                
                callBack(isSuccess: true)
            } else {
                
                callBack(isSuccess: false)
            }
            
            print(userAcount)
        }
        
        
        
        
        
    }
    

}
